<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="模块使用,office," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="使用xlrd模块读取Excel文档，仅限于.xls文件">
<meta property="og:type" content="article">
<meta property="og:title" content="(转载)python操作Excel读写--使用xlrd">
<meta property="og:url" content="http://rainy008.github.io/2017/06/08/(转载) python操作Excel读写--使用xlrd/index.html">
<meta property="og:site_name" content="Rainy008">
<meta property="og:description" content="使用xlrd模块读取Excel文档，仅限于.xls文件">
<meta property="og:updated_time" content="2017-06-08T15:05:15.742Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="(转载)python操作Excel读写--使用xlrd">
<meta name="twitter:description" content="使用xlrd模块读取Excel文档，仅限于.xls文件">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>

  <title> (转载)python操作Excel读写--使用xlrd | Rainy008 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Rainy008</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">重拾梦想  茕茕前行</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                (转载)python操作Excel读写--使用xlrd
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2017-06-08T23:02:43+08:00" content="2017-06-08">
              2017-06-08
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2017/06/08/(转载) python操作Excel读写--使用xlrd/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2017/06/08/(转载) python操作Excel读写--使用xlrd/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>原文网址：<a href="http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html" target="_blank" rel="external">http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html</a>) </p>
<h1 id="一、安装xlrd模块"><a href="#一、安装xlrd模块" class="headerlink" title="一、安装xlrd模块"></a>一、安装xlrd模块</h1><p>   到python官网下载<a href="http://pypi.python.org/pypi/xlrd模块安装，前提是已经安装了python" target="_blank" rel="external">http://pypi.python.org/pypi/xlrd模块安装，前提是已经安装了python</a> 环境。</p>
<h1 id="二、使用介绍"><a href="#二、使用介绍" class="headerlink" title="二、使用介绍"></a>二、使用介绍</h1><p>  1、导入模块<br>      import xlrd<br>   2、打开Excel文件读取数据<br>       data = xlrd.open_workbook(‘excelFile.xls’)<br>   3、使用技巧<br>        获取一个工作表</p>
<pre><code>table = data.sheets()[0]          #通过索引顺序获取

table = data.sheet_by_index(0) #通过索引顺序获取

table = data.sheet_by_name(u&apos;Sheet1&apos;)#通过名称获取

获取整行和整列的值（数组）
</code></pre><p> 　　<br>         table.row_values(i)</p>
<pre><code> table.col_values(i)

获取行数和列数
</code></pre><p>　　<br>        nrows = table.nrows</p>
<pre><code>  ncols = table.ncols

  循环行列表数据
  for i in range(nrows ):
print table.row_values(i)
</code></pre><p>单元格<br>cell_A1 = table.cell(0,0).value</p>
<p>cell_C4 = table.cell(2,3).value</p>
<p>使用行列索引<br>cell_A1 = table.row(0)[0].value</p>
<p>cell_A2 = table.col(1)[0].value</p>
<p>简单的写入<br>row = 0</p>
<p>col = 0</p>
<p>类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error</p>
<p>ctype = 1 value = ‘单元格的值’</p>
<p>xf = 0 # 扩展的格式化</p>
<p>table.put_cell(row, col, ctype, value, xf)</p>
<p>table.cell(0,0)  #单元格的值’</p>
<p>table.cell(0,0).value #单元格的值’</p>
<h1 id="三、Demo代码"><a href="#三、Demo代码" class="headerlink" title="三、Demo代码"></a>三、Demo代码</h1><p>   Demo代码其实很简单，就是读取Excel数据。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div></pre></td><td class="code"><pre><div class="line">1 # -*- coding: utf-8 -*- </div><div class="line"> 2 import  xdrlib ,sys</div><div class="line"> 3 import xlrd</div><div class="line"> 4 def open_excel(file= &apos;file.xls&apos;):</div><div class="line"> 5     try:</div><div class="line"> 6         data = xlrd.open_workbook(file)</div><div class="line"> 7         return data</div><div class="line"> 8     except Exception,e:</div><div class="line"> 9         print str(e)</div><div class="line">10 #根据索引获取Excel表格中的数据   参数:file：Excel文件路径     colnameindex：表头列名所在行的所以  ，by_index：表的索引</div><div class="line">11 def excel_table_byindex(file= &apos;file.xls&apos;,colnameindex=0,by_index=0):</div><div class="line">12     data = open_excel(file)</div><div class="line">13     table = data.sheets()[by_index]</div><div class="line">14     nrows = table.nrows #行数</div><div class="line">15     ncols = table.ncols #列数</div><div class="line">16     colnames =  table.row_values(colnameindex) #某一行数据 </div><div class="line">17     list =[]</div><div class="line">18     for rownum in range(1,nrows):</div><div class="line">19 </div><div class="line">20          row = table.row_values(rownum)</div><div class="line">21          if row:</div><div class="line">22              app = &#123;&#125;</div><div class="line">23              for i in range(len(colnames)):</div><div class="line">24                 app[colnames[i]] = row[i] </div><div class="line">25              list.append(app)</div><div class="line">26     return list</div><div class="line">27 </div><div class="line">28 #根据名称获取Excel表格中的数据   参数:file：Excel文件路径     colnameindex：表头列名所在行的所以  ，by_name：Sheet1名称</div><div class="line">29 def excel_table_byname(file= &apos;file.xls&apos;,colnameindex=0,by_name=u&apos;Sheet1&apos;):</div><div class="line">30     data = open_excel(file)</div><div class="line">31     table = data.sheet_by_name(by_name)</div><div class="line">32     nrows = table.nrows #行数 </div><div class="line">33     colnames =  table.row_values(colnameindex) #某一行数据 </div><div class="line">34     list =[]</div><div class="line">35     for rownum in range(1,nrows):</div><div class="line">36          row = table.row_values(rownum)</div><div class="line">37          if row:</div><div class="line">38              app = &#123;&#125;</div><div class="line">39              for i in range(len(colnames)):</div><div class="line">40                 app[colnames[i]] = row[i]</div><div class="line">41              list.append(app)</div><div class="line">42     return list</div><div class="line">43 </div><div class="line">44 def main():</div><div class="line">45    tables = excel_table_byindex()</div><div class="line">46    for row in tables:</div><div class="line">47        print row</div><div class="line">48 </div><div class="line">49    tables = excel_table_byname()</div><div class="line">50    for row in tables:</div><div class="line">51        print row</div><div class="line">52 </div><div class="line">53 if __name__==&quot;__main__&quot;:</div><div class="line">54     main()</div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/path/to/wechat-reward-image" alt="Rainy008 WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="/path/to/alipay-reward-image" alt="Rainy008 Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/模块使用/" rel="tag">#模块使用</a>
          
            <a href="/tags/office/" rel="tag">#office</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/08/(转载) pycharm快捷键及一些常用设置/" rel="next" title="(转载)pycharm快捷键及一些常用设置">
                <i class="fa fa-chevron-left"></i> (转载)pycharm快捷键及一些常用设置
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2017/06/08/(转载) python操作Excel读写--使用xlrd/"
           data-title="(转载)python操作Excel读写--使用xlrd" data-url="http://rainy008.github.io/2017/06/08/(转载) python操作Excel读写--使用xlrd/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Rainy008" />
          <p class="site-author-name" itemprop="name">Rainy008</p>
          <p class="site-description motion-element" itemprop="description">留下记忆足迹，记录所学所想</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">8</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一、安装xlrd模块"><span class="nav-text">一、安装xlrd模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、使用介绍"><span class="nav-text">二、使用介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、Demo代码"><span class="nav-text">三、Demo代码</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Rainy008</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"rainy008"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
  





  
  
  

  

  

</body>
</html>
